﻿using System.Data;
using Ortodoctor.modelo;
using Ortodoctor.Componentes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ortodoctor.Agenda
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write("<script>alert('Mensagem!');</script>");
            
        }

        [WebMethod]
        public static string getAllClient()
        {
            modeloCliente modelo = new modeloCliente();
            DataTable dt = modelo.getAll();
            string s = "<table id=\"cliente_agenda\" style=\"width: 100%\"><tr>" +
                       "<th class=\"cliente_agenda_header\">COD</th>" +
                       "<th class=\"cliente_agenda_header\">Nome</th>" +
                       "<th class=\"cliente_agenda_header\">CPF</th>" +
                       "<th class=\"cliente_agenda_header\">RG</th>" +
                       "</tr>";
            int cont = 1;
            foreach (DataRow dr in dt.Rows)
            {
                s += "<tr onclick=\"select(this.id)\" id=\"rowC"+cont+"\" class=\"rows_clientes\"><td class=\"classtd\" style=\"height: 5px\">" + (string.IsNullOrEmpty(s) ? dr["id_cliente"].ToString() : dr["id_cliente"].ToString()) + "</td>";
                s += "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["nome"].ToString() : dr["nome"].ToString()) + "</td>";
                s += "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["cpf"].ToString() : dr["cpf"].ToString()) + "</td>";
                s += "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["rg"].ToString() : dr["rg"].ToString()) + "</td>";
                s += "</tr>";
                ++cont;
            }
            s += "</table>";
            return s;
        }

        [WebMethod]
        public static string getAllProcedimento()
        {
            modeloProcedimento modelo = new modeloProcedimento();
            DataTable dt = modelo.getAll();
            string s = "<table id=\"procedimento_agenda\" style=\"width: 100%\"><tr>" +
                       "<th class=\"procedimento_agenda_header\">COD</th>" +
                       "<th class=\"procedimento_agenda_header\">Nome</th>" +
                       "<th class=\"procedimento_agenda_header\">Tipo</th>" +
                       "</tr>";
            int cont = 1;
            foreach (DataRow dr in dt.Rows)
            {
                s += "<tr onclick=\"select(this.id)\" id=\"rowP" + cont + "\" class=\"rows_procedimentos\">" +
                     "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["id_procedimento"].ToString() : dr["id_procedimento"].ToString()) + "</td>";
                s += "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["nome_procedimento"].ToString() : dr["nome_procedimento"].ToString()) + "</td>";
                s += "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["tipo"].ToString() : dr["tipo"].ToString()) + "</td>";
                s += "</tr>";
                ++cont;
            }
            s += "</table>";
            return s;
        }

        [WebMethod]
        public static int saveProcedimentoCliente(int id_cliente, int id_procedimento)
        {
            modeloProcedimentoCliente mpc = new modeloProcedimentoCliente();
            Procedimento_Cliente pc = new Procedimento_Cliente();
            pc.IdCliente = id_cliente;
            pc.IdProcedimento = id_procedimento;
            int id_proc_cliente = mpc.insert(pc);
            return id_proc_cliente;
        }

        [WebMethod]
        public static void saveAgenda(int id_procedimento_cliente, int id_dentista, String hora, int id_clinica, String observacoes, String dia)
        {
            modeloAgenda ma = new modeloAgenda();
            modeloDentista md = new modeloDentista();
            modeloProcedimentoCliente mpc = new modeloProcedimentoCliente();
            Ortodoctor.Componentes.Agenda a = new Ortodoctor.Componentes.Agenda();
            ModeloClinica mc = new ModeloClinica();

            //a.Clinica.IdClinica = id_clinica;
            //a.Observacoes = observacoes;
            //a.Dentista.IdDentista = id_dentista;
            //a.DataAgenda = dia;
            //a.ProcedimentoCliente.IdProcedimentoCliente = id_procedimento_cliente;

            //a.Hora = hora;
            //ma.insert2(a);
        }

        [WebMethod]
        public static string getAgendasDia(string dia, string dentista)
        {
            modeloAgenda ma = new modeloAgenda();
            DataTable dt = new DataTable();
            dt = ma.getAgendasDia(dia, dentista);
            String coluna = "";

            string linhas = "";
            foreach (DataRow dr in dt.Rows)
            {
                string colunas = "";
                foreach (DataColumn dc in dt.Columns)
                {
                    coluna = coluna + dc.ColumnName + "\n";
                    colunas += string.IsNullOrEmpty(colunas) ? dr[dc.ColumnName] : ">" + dr[dc.ColumnName];
                }
                linhas += colunas + "#";
            }
            return linhas;
        }

        [WebMethod]
        public static string getClientNome(int id)
        {
            modeloCliente mc = new modeloCliente();
            return (mc.getClientNome(id));
        }

        [WebMethod]
        public static string getAllDentista()
        {
            modeloDentista md = new modeloDentista();
            DataTable dt = md.getAll();
            string s = "<table id=\"dentista_agenda\" style=\"width: 100%\"><tr>" +
                       "<th class=\"dentistas_header\">COD</th>" +
                       "<th class=\"dentistas_header\">Nome</th>" +
                       "<th class=\"dentistas_header\">CPF</th>" +
                       "<th class=\"dentistas_header\">RG</th>" +
                       "</tr>";
            int cont = 1;
            foreach (DataRow dr in dt.Rows)
            {
                s += "<tr onclick=\"select(this.id)\" id=\"rowD" + cont + "\" class=\"rows_dentistas\">" +
                     "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["id_dentista"].ToString() : dr["id_dentista"].ToString()) + "</td>";
                s += "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["nome"].ToString() : dr["nome"].ToString()) + "</td>";
                s += "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["cpf"].ToString() : dr["cpf"].ToString()) + "</td>";
                s += "<td class=\"classtd\">" + (string.IsNullOrEmpty(s) ? dr["rg"].ToString() : dr["rg"].ToString()) + "</td>";
                s += "</tr>";
                ++cont;
            }
            s += "</table>";
            return s;
        }
    }
}